package com.ayou.plugin;

import com.ayou.executor.Executor;
import com.ayou.mapping.MappedStatement;

import java.util.Map;

/**
 * @description: 分页拦截器
 * @author: ayouman
 * @date: 2024/11/11 23:16
 */
@Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Map.class}))
public class LimitInterceptor implements Interceptor{

    @Override
    public Object intercept(Invocation invocation) {
        System.out.println("分页拦截器开始");
        MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
        String sql = ms.getSql();
        ms.setSql(sql + " limit 2");
        Object res = invocation.proceed();
        System.out.println("分页拦截器结束");
        return res;
    }

    @Override
    public <T> T plugin(Object target) {
        return Plugin.wrap(target, this);
    }
}
